package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.domain.TUsers;
import com.item.domain.TbUser;
import com.item.domain.TbUserComment;
import com.item.mapper.TbUserCommentInitMapper;
import com.item.mapper.TbUserCommentMapper;
import com.item.mapper.TbUserMapper;
import com.item.po.TbUserCommentInitPo;
import com.item.po.TbUserPo;
import com.item.service.TbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    TbUserCommentMapper tbUserCommentMapper;

    @Autowired
    TbUserCommentInitMapper tbUserCommentInitMapper;

    public List<TbUserPo> findAll(int limit, int page, TbUser tbUser) {
        Map<Integer, Integer> map = new HashMap<>();
        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        List<TbUserCommentInitPo> tbUserComments = tbUserCommentInitMapper.findCommentCountAll();
        List<TbUserCommentInitPo> tbUserStartss = tbUserCommentInitMapper.findStartCountAll();
        List<TbUserCommentInitPo> tbUserlikeCocunts = tbUserCommentInitMapper.findLikeCountAll();
        for (TbUserCommentInitPo tbUserComment : tbUserComments) {
            map.put(tbUserComment.getCommentId(), tbUserComment.getCommentCount());
        }
        for (TbUserCommentInitPo tbUserComment : tbUserStartss) {
            map1.put(tbUserComment.getCommentId(), tbUserComment.getStartCounts());
        }
        for (TbUserCommentInitPo tbUserComment : tbUserlikeCocunts) {
            map2.put(tbUserComment.getCommentId(), tbUserComment.getLikeCount());
        }

        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        List<TbUserPo> all2 = new ArrayList<>();
        List<TbUserPo> all = tbUserMapper.findAll(tbUser);
        for (TbUserPo tbUserPo : all) {
            TbUserPo tbUserPo1 = tbUserPo;
            List<TbUserComment> commentList = tbUserPo1.getCommentList();
            List<TbUserComment> commentList1 = new ArrayList<>();
            for (TbUserComment tbUserComment : commentList) {
                TbUserComment tbUserComment1 = tbUserComment;

                if (map.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setCommentCount(map.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setCommentCount(0);
                }
                if (map1.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setStartCount(map1.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setStartCount(0);
                }
                if (map2.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setLikeCount(map2.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setLikeCount(0);
                }
                commentList1.add(tbUserComment1);
                tbUserPo1.setCommentList(commentList1);
                break;
            }
            all2.add(tbUserPo1);
        }
        return all2;
    }


    public int add(TbUser tbUser) {

        return tbUserMapper.insertSelective(tbUser);
    }


    public int update(TbUser tbUser) {
        return tbUserMapper.updateByPrimaryKeySelective(tbUser);
    }

    @Override
    public int delete(int id) {
        return tbUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbUser getUserId(int id) {
        return tbUserMapper.getUserId(id);
    }

    @Override
    public TbUser findAllUsers(String phone) {
        TbUser users = tbUserMapper.selectByPrimaryKeyPhone(phone);
        if (users == null) {
            TbUser users1 = new TbUser();
            users1.setMobile(phone);
            int i = tbUserMapper.insertSelective(users1);
            if (i == 1) {
                users = tbUserMapper.selectByPrimaryKeyPhone(phone);
            }
        }
        return users;
    }

    @Override
    public int userInfoAddMobile(TbUser users) {
        int i = 0;
        try {
            i = tbUserMapper.insertSelective(users);
            return i;
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public List<TbUser> getUserList(int userId) {
        return tbUserMapper.selectAllUserId(userId);
    }

    @Override
    public TbUserPo tbUserUserInfoList(int id) {
        TbUserPo tbUserPo = tbUserMapper.selectFindById(id);
        if (tbUserPo != null) {
            List<TbUserComment> tbUserComments = tbUserCommentMapper.selectByUserId(tbUserPo.getId());
            if (tbUserComments != null) {
                tbUserPo.setCommentList(tbUserComments);
            } else {
                tbUserPo.setCommentList(new ArrayList<>());
            }
            return tbUserPo;
        } else {
            return new TbUserPo();
        }
    }

    @Override
    public List<TbUser> idsArray(List<String> stringList) {

        return tbUserMapper.idsArray(stringList);
    }

    @Override
    public List<TbUser> tbUserName(TbUser name) {

        return tbUserMapper.selectUserName(name);
    }

    @Override
    public List<TbUserPo> findAllList(int limit, int page, TbUser tbUser) {
        Map<Integer, Integer> map = new HashMap<>();
        Map<Integer, Integer> map1 = new HashMap<>();
        Map<Integer, Integer> map2 = new HashMap<>();
        List<TbUserCommentInitPo> tbUserComments = tbUserCommentInitMapper.findCommentCountAll();
        List<TbUserCommentInitPo> tbUserStartss = tbUserCommentInitMapper.findStartCountAll();
        List<TbUserCommentInitPo> tbUserlikeCocunts = tbUserCommentInitMapper.findLikeCountAll();
        for (TbUserCommentInitPo tbUserComment : tbUserComments) {
            map.put(tbUserComment.getCommentId(), tbUserComment.getCommentCount());
        }
        for (TbUserCommentInitPo tbUserComment : tbUserStartss) {
            map1.put(tbUserComment.getCommentId(), tbUserComment.getStartCounts());
        }
        for (TbUserCommentInitPo tbUserComment : tbUserlikeCocunts) {
            map2.put(tbUserComment.getCommentId(), tbUserComment.getLikeCount());
        }

        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        List<TbUserPo> all2 = new ArrayList<>();
        List<TbUserPo> all = tbUserMapper.findAllList(tbUser);
        for (TbUserPo tbUserPo : all) {
            TbUserPo tbUserPo1 = tbUserPo;
            List<TbUserComment> commentList = tbUserPo1.getCommentList();
            List<TbUserComment> commentList1 = new ArrayList<>();
            for (TbUserComment tbUserComment : commentList) {
                TbUserComment tbUserComment1 = tbUserComment;

                if (map.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setCommentCount(map.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setCommentCount(0);
                }
                if (map1.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setStartCount(map1.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setStartCount(0);
                }
                if (map2.get(tbUserComment.getId()) != null) {
                    tbUserComment1.setLikeCount(map2.get(tbUserComment.getId()));
                } else {
                    tbUserComment1.setLikeCount(0);
                }
                commentList1.add(tbUserComment1);
                tbUserPo1.setCommentList(commentList1);
            }
            all2.add(tbUserPo1);
        }
        return all2;
    }

    @Override
    public TbUserPo getUserCountId(int id) {
        TbUserPo tbUserPo = tbUserMapper.selectFindById(id);
        int countfensi = tbUserMapper.getUserCountFensi(id);
        int countguanzhu = tbUserMapper.getUserCountGuanZhu(id);
        int countXiHuan = tbUserMapper.getUserCountXiHuan(id);
        tbUserPo.setCountfensi(countfensi);
        tbUserPo.setCountguanzhu(countguanzhu);
        tbUserPo.setCountXiHuan(countXiHuan);
        return tbUserPo;
    }

    @Override
    public TbUser tbUserBeauty(int id) {
//        List<Integer> tbUser = tbUserMapper.tbUserBeauty(id);
//        String strings = "";
//        for (Integer integer : tbUser) {
//            strings+=integer;
//        }
//       // Integer[] tbUser1 = (Integer[]) tbUser.toString();
//        Random genderator = new Random();
//        int randomIndex = genderator.nextInt(strings.length());
//        String str = tbUser.toArray()[randomIndex].toString();
        try {
            Thread.sleep(100);
            //TbUser tbUser1 = tbUserMapper.getUserId(Integer.valueOf(str));
            TbUser tbUser1 = tbUserMapper.getUserId(5);
            return tbUser1;
        } catch (InterruptedException e) {
            return new TbUser();
        }

    }


}
